<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class c_main_class extends AWS_CONTROLLER
{
	public function get_access_rule()
	{
		$rule_action["rule_type"] = 'white'; //黑名单,黑名单中的检查  'white'白名单,白名单以外的检查
		$rule_action["actions"] = array();
		return $rule_action;
	}

	public function setup()
	{
		$this->crumb('信息发布', '/publish/');
	}

	public function index_action()
	{
		if ($_GET['question_id'])
		{
			if (!$question_info = $this->model('question')->get_question_info_by_id(intval($_GET['question_id'])))
			{
				H::redirect_msg("指定问题不存在", '/question/' . $_GET['question_id']);
			}
			
			if (!$this->user_info['permission']['is_administortar'] AND !$this->user_info['permission']['is_moderator'])
			{
				if ($question_info['published_uid'] != $this->user_id OR !$this->user_info['permission']['edit_question'])
				{
					H::redirect_msg("你没有权限编辑这个问题", '/question/' . $_GET['question_id']);
				}
				
				if ((time() - $question_info['add_time']) > intval(get_setting('question_edit_time')) * 60)
				{
					H::redirect_msg("对不起，发布问题超过 " . get_setting('question_edit_time') . " 分钟以后无法修改", '/question/' . $_GET['question_id']);
				}
			}
			
			TPL::assign('question_info', $question_info);
		}
		else if ($this->is_post())
		{
			TPL::assign('question_info', array(
				'question_content' => $_POST['question_content'],
				'question_detail' => $_POST['question_detail']
			));
			
			$question_info['category_id'] = $_POST['category_id'];
		}
		else
		{
			if (!$this->user_info['permission']['publish_question'] AND !$this->user_info['permission']['is_administortar'] AND !$this->user_info['permission']['is_moderator'])
			{
				H::redirect_msg('你没有权限发布问题');
			}
		}
		
		TPL::assign('attach_access_key', md5($this->user_id . time()));
		
		TPL::assign('topic_tree', $this->model('topic')->get_topic_tree_list(5));
		
		if (!$question_info['category_id'] AND $_GET['category_id'])
		{
			$question_info['category_id'] = $_GET['category_id'];
		}
		
		TPL::assign('question_category_list', $this->model('system')->build_category_html('question', 0, $question_info['category_id']));
		
		TPL::assign('human_valid', human_valid('question_valid_hour'));
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::output('publish/index');
	}
}